//
//  AdCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/29/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit

final class ListTile: UIView {
    
    private lazy var textLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 13, weight: .regular)
        label.textColor = .greyColor
        label.textAlignment = .left
        label.sizeToFit()
        return label
    }()
    
    private lazy var iconView: UIImageView = {
        let icon = UIImageView()
        icon.contentMode = .scaleAspectFit
        return icon
    }()
    
    public func updateUI (image: UIImage? = nil, text: String? = nil) {
        if let _image = image {
            self.iconView.image = _image
        }
        
        if let _text = text {
            self.textLabel.text = _text
        }
    }
    
    init () {
        super.init(frame: .zero)
        self.addSubview(textLabel)
        self.addSubview(iconView)
        
        iconView.snp.makeConstraints { (make) in
            make.width.equalTo(16)
            make.height.equalTo(12)
            make.centerY.leading.equalToSuperview()
        }
        textLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(iconView.snp.trailing).offset(8)
            make.trailing.equalToSuperview()
            make.centerY.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


final class AdCell: UITableViewCell {
    
    // MARK: - Outlets
    
    private lazy var separatorView: UIView = {
        let view = UIView()
        view.backgroundColor = .greyBlue
        return view
    }()
    
    private lazy var titleLbl: UILabel = {
        let lbl = UILabel()
        lbl.textAlignment = .left
        lbl.numberOfLines = 0
        lbl.font = UIFont.systemFont(ofSize: 17, weight: .bold)
        return lbl
    }()
    
    private lazy var carImageView: UIImageView = {
        let iView = UIImageView()
        iView.contentMode = .scaleAspectFill
        iView.clipsToBounds = true
        iView.layer.cornerRadius = 5
        return iView
    }()
    
    private lazy var priceLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont.systemFont(ofSize: 20, weight: .bold)
        lbl.textColor = .black
        return lbl
    }()
    
    private lazy var statusLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont.systemFont(ofSize: 13, weight: .medium)
        lbl.textAlignment = .left
        return lbl
    }()
    
    private lazy var dividerView: UIView = {
        let view = UIView()
        view.backgroundColor = .greyBlue
        return view
    }()
    
    lazy var menuBtn: UIButton = {
        let btn = UIButton()
        btn.contentEdgeInsets = UIEdgeInsets(top: 12.5, left: 8.5, bottom: 12.5, right: 8.5)
        btn.setImage(UIImage(named: "menu"), for: .normal)
        return btn
    }()
    
    // MARK: - Tarif
    //private lazy var leftDaysItem = ListTile()
    private lazy var visCountItem = ListTile()
    
    // MARK: - Properties
    
    var adCellModel: AdCellModel! {
        didSet {
            titleLbl.attributedText = adCellModel.name
            priceLbl.text = adCellModel.priceText
            visCountItem.updateUI(text: adCellModel.visText)
            //leftDaysItem.updateUI(text: adCellModel.leftDaysText)
            statusLbl.textColor = adCellModel.statusColor
            statusLbl.text = adCellModel.statusText
            contentView.alpha = adCellModel.isCellDarken ? 0.6 : 1
            
            if let wallPaper = adCellModel.wallpaper {
                self.carImageView.sd_setImage(with: URL(string: wallPaper))
            } else {
                self.carImageView.image = nil
            }
            
            self.themeCell(isDark: adCellModel.postMarketing.colored)
        }
    }
    
    
    // MARK: - Methods
    
    private func createDetailsText (strong: String, normal: String) -> NSMutableAttributedString  {
        let attributedString = NSMutableAttributedString(string: " " + normal)
        let attrs = [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 17)]
        let boldString = NSMutableAttributedString(string: strong, attributes:attrs)
        boldString.append(attributedString)
        return boldString
    }
    
    private func themeCell (isDark: Bool) {
        contentView.backgroundColor = isDark ? .darkBlueColor : .white
        textLabel?.textColor = isDark ? .white : .black
        priceLbl.textColor = isDark ? .white : .black
    }
    
    // MARK: - Init
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    
    private func addViews () {
        //leftDaysItem.updateUI(image: UIImage(named: "eye"))
        visCountItem.updateUI(image: UIImage(named: "eye"))
        self.contentView.addSubview(separatorView)
        self.contentView.addSubview(titleLbl)
        self.contentView.addSubview(priceLbl)
        //self.contentView.addSubview(leftDaysItem)
        self.contentView.addSubview(visCountItem)
        self.contentView.addSubview(carImageView)
        self.contentView.addSubview(statusLbl)
        self.contentView.addSubview(menuBtn)
        self.contentView.addSubview(dividerView)
        
        separatorView.snp.makeConstraints { (make) in
            make.top.leading.trailing.equalToSuperview()
            make.height.equalTo(12)
        }
        titleLbl.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.equalTo(separatorView.snp.bottom).offset(16)
        }
        carImageView.snp.makeConstraints { (make) in
            make.width.equalTo(150)
            make.height.equalTo(84)
            make.trailing.equalToSuperview().inset(16)
            make.top.equalTo(titleLbl.snp.bottom).offset(12)
        }
        priceLbl.snp.makeConstraints { (make) in
            make.top.equalTo(carImageView)
            make.leading.equalTo(titleLbl)
            make.trailing.equalTo(carImageView.snp.leading).offset(-12)
            make.height.equalTo(30)
        }
//        leftDaysItem.snp.makeConstraints { (make) in
//            make.top.equalTo(priceLbl.snp.bottom)
//            make.leading.equalTo(titleLbl)
//            make.trailing.equalTo(priceLbl)
//            make.height.equalTo(24)
//        }
        visCountItem.snp.makeConstraints { (make) in
            make.top.equalTo(priceLbl.snp.bottom)
            make.leading.equalTo(titleLbl)
            make.trailing.equalTo(priceLbl)
            make.height.equalTo(24)
        }
        dividerView.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.height.equalTo(1)
            make.top.equalTo(carImageView.snp.bottom).offset(12)
        }
        statusLbl.snp.makeConstraints { (make) in
            make.top.equalTo(dividerView.snp.bottom).offset(16)
            make.height.equalTo(24)
            make.leading.equalTo(titleLbl)
        }
        menuBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(statusLbl)
            make.width.equalTo(40)
            make.height.equalTo(30)
            make.trailing.equalToSuperview().offset(-16)
            make.bottom.equalToSuperview().offset(-24)
        }
    }
}
